package com.zhao.fishingguideserver.controller;

import com.zhao.fishingguideserver.POJO.Comment;
import com.zhao.fishingguideserver.POJO.Result;
import com.zhao.fishingguideserver.service.CommentService;
import com.zhao.fishingguideserver.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 发表评论
     */
    @PostMapping("/create")
    public Result<String> createComment(@RequestBody Comment comment) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        if (claims == null || claims.get("id") == null) {
            return Result.error("用户未登录");
        }
        Integer userId = Integer.parseInt(claims.get("id").toString());
        comment.setUserId(userId);
        
        String message = commentService.createComment(comment);
        return message.contains("成功") ? Result.success(message) : Result.error(message);
    }

    /**
     * 更新评论
     */
    @PutMapping("/update")
    public Result<String> updateComment(@RequestBody Comment comment) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        if (claims == null || claims.get("id") == null) {
            return Result.error("用户未登录");
        }
        Integer userId = Integer.parseInt(claims.get("id").toString());
        
        // 验证评论是否属于当前用户
        Comment existingComment = commentService.getCommentById(comment.getId());
        if (existingComment == null) {
            return Result.error("评论不存在");
        }
        if (!existingComment.getUserId().equals(userId)) {
            return Result.error("无权限修改此评论");
        }
        
        String message = commentService.updateComment(comment);
        return message.contains("成功") ? Result.success(message) : Result.error(message);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/delete/{commentId}")
    public Result<String> deleteComment(@PathVariable Integer commentId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        if (claims == null || claims.get("id") == null) {
            return Result.error("用户未登录");
        }
        Integer userId = Integer.parseInt(claims.get("id").toString());
        
        String message = commentService.deleteComment(commentId, userId);
        return message.contains("成功") ? Result.success(message) : Result.error(message);
    }

    /**
     * 获取评论详情
     */
    @GetMapping("/detail/{commentId}")
    public Result<Comment> getCommentDetail(@PathVariable Integer commentId) {
        Comment comment = commentService.getCommentById(commentId);
        if (comment == null) {
            return Result.error("评论不存在");
        }
        return Result.success(comment);
    }

    /**
     * 根据帖子ID获取评论列表
     */
    @GetMapping("/post/{postId}")
    public Result<Map<String, Object>> getCommentsByPostId(
            @PathVariable Integer postId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        List<Comment> comments = commentService.getCommentsByPostId(postId, page, size);
        int total = commentService.getCommentCountByPostId(postId);
        
        Map<String, Object> data = new HashMap<>();
        data.put("comments", comments);
        data.put("total", total);
        data.put("page", page);
        data.put("size", size);
        data.put("totalPages", (total + size - 1) / size);
        data.put("postId", postId);
        
        return Result.success(data);
    }

    /**
     * 根据用户ID获取评论列表
     */
    @GetMapping("/user/{userId}")
    public Result<Map<String, Object>> getCommentsByUserId(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        List<Comment> comments = commentService.getCommentsByUserId(userId, page, size);
        int total = commentService.getCommentCountByUserId(userId);
        
        Map<String, Object> data = new HashMap<>();
        data.put("comments", comments);
        data.put("total", total);
        data.put("page", page);
        data.put("size", size);
        data.put("totalPages", (total + size - 1) / size);
        data.put("userId", userId);
        
        return Result.success(data);
    }

    /**
     * 点赞/取消点赞评论
     */
    @PostMapping("/like/{commentId}")
    public Result<String> toggleLike(@PathVariable Integer commentId) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        if (claims == null || claims.get("id") == null) {
            return Result.error("用户未登录");
        }
        Integer userId = Integer.parseInt(claims.get("id").toString());
        
        String message = commentService.toggleLike(commentId, userId);
        return message.contains("成功") ? Result.success(message) : Result.error(message);
    }
}
